package com.domor.xxx.system.controller.system;

import com.domor.xxx.common.core.controller.BaseController;
import com.domor.xxx.framework.shiro.util.ShiroUtils;
import com.domor.xxx.system.domain.SysDept;
import com.domor.xxx.system.service.SysDeptService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * <h3>部门管理控制层</h3>
 * @author liyuyang
 * @date 2020/4/22
 **/
@Controller
@RequestMapping("/system/dept")
public class SysDeptController extends BaseController {

    private String prefix = "/system/dept";

    @Autowired
    private SysDeptService service;

    /**
     * 部门管理-首页
     */
    @GetMapping()
    public String index() {
        return prefix + "/index";
    }

    /**
     * 部门管理-增加页
     */
    @GetMapping("/add")
    @RequiresPermissions("system:dept:add")
    public String addGet(Model model, int parentId) {
        SysDept dept = new SysDept();
        dept.setParentId(parentId == 0 ? 1 : parentId);
        model.addAttribute("dept", dept);
        return prefix + "/add";
    }

    /**
     * 部门管理-修改页
     */
    @GetMapping("/edit")
    @RequiresPermissions("system:dept:edit")
    public String editGet(Model model, int deptId) {
        model.addAttribute("dept", service.getById(deptId));
        return prefix + "/edit";
    }

    /**
     * 部门管理-首页列表查询
     */
    @ResponseBody
    @RequiresPermissions("system:dept:list")
    @RequestMapping("/list")
    public Object list(int status) {
        List data = service.queryTreeGrid(status);
        return getDataTable(data);
    }

    /**
     * 部门管理-部门树
     */
    @ResponseBody
    @RequestMapping("/tree")
    public Object tree() {
        return service.queryTree(0);
    }

    /**
     * 部门管理-新增
     */
    @ResponseBody
    @PostMapping("insert")
    public Object insert(SysDept dept) {
        dept.setCreateBy(ShiroUtils.getLoginName());
        int row = service.insert(dept);
        return toAjax(row);
    }

    /**
     * 部门管理-修改
     */
    @ResponseBody
    @PostMapping("update")
    public Object update(SysDept dept) {
        dept.setUpdateBy(ShiroUtils.getLoginName());
        int row = service.update(dept);
        return toAjax(row);
    }

    /**
     * 部门管理-删除
     */
    @ResponseBody
    @RequestMapping("/delete")
    @RequiresPermissions("system:dept:delete")
    public Object delete(int deptId) {
        service.delete(deptId);
        return success();
    }

}
